/* *
 * @Author: chenqinglong
 * @Description: 任务认领公共方法
 * @Date: 2018/5/31 15:23
 * @Param: datagridId 列表id ，searchFormId 查询表单id
 *         woType工单类型 见常量，codeSymoble：0，取woCode,1:取code
 * @return:
 */
function claimTask(datagridId,searchFormId,woType,codeSymoble) {
    //选中的任务id
    var checkedtaskIds = [];
    var checkedWoCodes =[];
    //获取选中行
    var rows = $("#"+datagridId).datagrid("getChecked");
    if(rows.length<=0){
        showInfoMsg("请选择您要认领的数据");
        return;
    }else{
        $.each(rows, function(index, item){
            checkedtaskIds.push(item.taskId);
            if(codeSymoble=="0"){
                checkedWoCodes.push(item.woCode);
            }else{
                checkedWoCodes.push(item.code);
            }
        });
        checkedtaskIds =checkedtaskIds.join(",");
        checkedWoCodes =checkedWoCodes.join(",");
    }
    //console.log(checkedtaskIds);
    confirmMsg('您确定要认领选中的任务吗？', function(taskIds,woCodes,woType) {
        var data = {
            taskIds: taskIds,
            woCodes: woCodes,
            woType:  woType
        }
        ajaxLoading("认领中，请稍后。。。");
        uceAjax(G_CTX_PATH + "/cs/CsCommon/claim.do", data, function(data){
            $('#'+datagridId).datagrid('load', serializeFormObj(searchFormId));
            ajaxLoadEnd();
        },"POST");
    }, [checkedtaskIds,checkedWoCodes,woType]);
}


/**
 * 初始化分配
 */
function initDesignate(woType) {

    $(document.body).append(
        '<div id="commonDesignate">' +
        '    <div id="designateWin" class="easyui-dialog" style="width: 450px; height: 300px; padding: 5px" closed="true" buttons="#divDesignateBtn">' +
        '        <div class="easyui-layout" data-options="fit:true">' +
        '            <div data-options="region:\'center\',border:false"' +
        '                 style="background: #fff;">' +
        '                <form id="formDesignateForm" method="post" class="hgform">' +
        '                    <input type="hidden" id="checkedtaskIds" />' +
        '                    <input type="hidden" id="checkedWoCodes" />' +
        '                    <input type="hidden" id="searchFormId" />' +
        '                    <input type="hidden" id="datagridId">' +
        '                    <input type="hidden" id="woType" />' +
        '' +
        '                    <table class="table" style="width: 400px; border: 0px;">' +
        '                        <tbody>' +
        '                            <tr style="padding-left: 20px">' +
        '                                <td width="80px">分配人员:</td>' +
        '                                <td width="300px"><input name="targetEmpCode" id="targetEmpCode" style="width: 100%"' +
        '                                                         class="easyui-combobox" required="true" data-options="prompt:\'请选择分配人员\'"></td>' +
        '                            </tr>' +
        '                        </tbody>' +
        '                    </table>' +
        '                </form>' +
        '            </div>' +
        '        </div>' +
        '    </div>' +
        ' ' +
        '   <div id="divDesignateBtn">' +
        '        <a href="javascript:void(0)" class="easyui-linkbutton  save" onclick="enterDesignate(this, 0)" >确定</a>' +
        '        <a href="javascript:void(0)" class="easyui-linkbutton  save" onclick="javascript:closeDialog(\'designateWin\')" >取消</a>' +
        '    </div>' +
        '</div>');

    $.parser.parse('#commonDesignate');


    //初始化转派人
    $('#designateWin #targetEmpCode').combobox({
        url:G_CTX_PATH+'/cs/omg/emp/getCSEmps.do',
        queryParams:{
            woType: woType
        },
        valueField:'empCode',
        textField:'empName',
        multiple: true,
        editable: false,
        formatter: function (row) {
            var opts = $(this).combobox('options');
            return '<input type="checkbox" class="combobox-checkbox">' + row[opts.textField] + '[' + row[opts.valueField] + ']'
        },
        onSelect: function (row) {
            //console.log(row);
            var opts = $(this).combobox('options');
            var el = opts.finder.getEl(this, row[opts.valueField]);
            el.find('input.combobox-checkbox')._propAttr('checked', true);
        },
        onUnselect: function (row) {
            var opts = $(this).combobox('options');
            var el = opts.finder.getEl(this, row[opts.valueField]);
            el.find('input.combobox-checkbox')._propAttr('checked', false);
        }
    });
}

/**
 *
 * @author: may
 * @date: 2018年9月13日10:27:24
 * @description: 分配工单到指定人员
 * @param datagridId 列表ID
 * @param searchFormId 表单ID 用于刷新列表
 * @param woType 工单类型
 * @param 0:woCode, 1：code
 *
 */
function designate(datagridId,searchFormId,woType,codeSymoble) {
    // //初始化转派人
    // $('#designateWin #targetEmpCode').combobox({
    //     url:G_CTX_PATH+'/cs/omg/emp/getCSEmpsOfNotSelf.do',
    //     valueField:'empCode',
    //     textField:'empName'
    // });
    //选中的任务id
    var checkedtaskIds = [];
    var checkedWoCodes =[];
    //获取选中行
    var rows = $("#"+datagridId).datagrid("getChecked");
    if(rows.length<=0){
        showInfoMsg("请选择您要认领的数据");
        return;
    }else{
        $.each(rows, function(index, item){
            checkedtaskIds.push(item.taskId);
            if(codeSymoble=="0"){
                checkedWoCodes.push(item.woCode);
            }else{
                checkedWoCodes.push(item.code);
            }
        });
        checkedtaskIds =checkedtaskIds.join(",");
        checkedWoCodes =checkedWoCodes.join(",");
    }

    $('#designateWin #checkedtaskIds').val(checkedtaskIds);
    $('#designateWin #checkedWoCodes').val(checkedWoCodes);
    $('#designateWin #searchFormId').val(searchFormId);
    $('#designateWin #woType').val(woType);
    $('#designateWin #codeSymoble').val(codeSymoble);
    $('#designateWin #datagridId').val(datagridId);
    $('#designateWin #targetEmpCode').combobox('clear');

    icsOpenDialog("designateWin", '分配认领人员', 300);
}

function enterDesignate(){
    var formCheck = $("#designateWin #formDesignateForm").form('validate');

    if(!formCheck){
        /*window.parent.rlliu*/showInfoMsg("请选择分配人员");
        return;
    }

    //前台处理字符串
    var empArray = $('#designateWin #targetEmpCode').combobox('getValues');
    var names = $('#designateWin #targetEmpCode').combobox('getText');
    var nameArray = names.split(',');

    var valueArray = [];
    for(var i = 0; i<nameArray.length; i++){
        valueArray.push({empName: nameArray[i], empCode: empArray[i]});
    }

    var data = {
        taskIds: $('#designateWin #checkedtaskIds').val(),
        woCodes: $('#designateWin #checkedWoCodes').val(),
        woType:  $('#designateWin #woType').val(),
        emps: JSON.stringify(valueArray)
    }


    ajaxLoading("分配中，请稍后。。。");
    uceAjax(G_CTX_PATH + "/cs/CsCommon/designateByMulite.do", data, function(data){
        closeDialog('designateWin')
        ajaxLoadEnd();
        $('#'+ $('#designateWin #datagridId').val()).datagrid('load', serializeFormObj($('#designateWin #searchFormId').val()));
    },"POST");

}

/**
 * 初始化指派
 */
function initDesignateToOrg(row, searchFormId, datagridId) {
    $(document.body).append(
        '<div id="commonDesignate">' +
        '    <div id="designateToOrgWin" class="easyui-dialog" style="width: 300px; height: 300px; padding: 5px" closed="true" buttons="#divDesignateBtn">' +
        '        <div class="easyui-layout" data-options="fit:true">' +
        '            <div data-options="region:\'center\',border:false"' +
        '                 style="background: #fff;">' +
        '                <form id="formDesignateToOrgForm" method="post" class="hgform">' +
        '                    <input type="hidden" id="woCode" value=""/>' +
        '                    <input type="hidden" id="pid" value=""/>' +
        '                    <input type="hidden" id="sourceTaskId" value=""/>' +
        '                    <input type="hidden" id="targetBaseOrgName" />' +
        '                    <input type="hidden" id="searchFormId" value=""/>' +
        '                    <input type="hidden" id="datagridId" value=""/>' +
        '                    <table class="table" style="width: 285px; border: 0px;">' +
        '                        <tbody>' +
        '                            <tr style="padding-left: 20px">' +
        '                                <td width="105px">指派机构：</td>' +
        '                                <td width="230px"><input name="targetBaseOrgCode" id="targetOrgCode" style="width: 90%"' +
        '															 class="easyui-combobox" required="true" data-options="prompt:\'请输入指派机构\'"></td>' +
        '                            </tr>' +
        '                            <tr style="padding-left: 20px">' +
        '                                <td width="105px">指派描述：</td>' +
        '                                <td width="230px"><textarea style="resize: none;float: left; border-bottom:1px solid #ccc; ' +
        '                                                               border-top:1px solid #ccc;border-right:1px solid #ccc;border-right:1px solid #ccc;" required="true"' +
        '                                                               id="dumpRemarks" name="dumpRemarks" cols=24 rows=8 placeholder="请输入指派描述" class="easyui-validatebox"/></textarea></td>' +
        '                            </tr>' +
        '                        </tbody>' +
        '                    </table>' +
        '                </form>' +
        '            </div>' +
        '        </div>' +
        '    </div>' +
        ' ' +
        '   <div id="divDesignateBtn">' +
        '        <a href="javascript:void(0)" class="easyui-linkbutton  save" onclick="doDesignateToOrg(this, 0)" >确定</a>' +
        '        <a href="javascript:void(0)" class="easyui-linkbutton  save" onclick="javascript:closeDialog(\'designateToOrgWin\')" >取消</a>' +
        '    </div>' +
        '</div>');
    $('#designateToOrgWin #dumpRemarks').val("");
    $('#designateToOrgWin #woCode').val(row.woCode);
    $('#designateToOrgWin #pid').val(row.pid);
    $('#designateToOrgWin #sourceTaskId').val(row.taskId);
    $('#designateToOrgWin #searchFormId').val(searchFormId);
    $('#designateToOrgWin #datagridId').val(datagridId);
    $.parser.parse('#commonDesignate');
    orgCombogrid('targetOrgCode', {queryParams: {searchOrgTypes: '20,30,40'}});
}

function doDesignateToOrg(){
    var formCheck = $("#designateToOrgWin #formDesignateToOrgForm").form('validate');

    if(!formCheck){
        return;
    }

    var dumpRemarks = $("#dumpRemarks").val();
    if (dumpRemarks.length > 1024){
        showInfoMsg("输入的指派描述长度不能超过1024个字符！");
        return;
    }

    var data = {
        woCode: $('#designateToOrgWin #woCode').val(),
        pid: $('#designateToOrgWin #pid').val(),
        sourceTaskId: $('#designateToOrgWin #sourceTaskId').val(),
        targetBaseOrgCode: $('#designateToOrgWin #targetOrgCode').combobox('getValue'),
        targetBaseOrgName: $('#designateToOrgWin #targetOrgCode').combobox('getText'),
        dumpRemarks: $('#designateToOrgWin #dumpRemarks').val()
    }

    ajaxLoading("指派中，请稍后。。。");
    uceAjax(G_CTX_PATH + "/cs/CsWoPddSer/saveDumpTask.do", data, function(data){
        debugger;
        if (data && data.success){
            showInfoMsg("指派成功！");
        } else {
            showInfoMsg(data.message);
        }
        closeDialog('designateToOrgWin');
        ajaxLoadEnd();
        $('#'+ $('#designateToOrgWin #datagridId').val()).datagrid('load', serializeFormObj($('#designateToOrgWin #searchFormId').val()));
    },"POST");
}


